package com.iap.admin.mediator
{
	import com.iap.admin.event.ImageEvent;
	import com.iap.admin.signal.ImageRequestSignal;
	import com.iap.admin.view.ImageView;
	import com.iap.admin.view.image.code.ImageEditViewCode;
	import com.iap.admin.view.image.ImageEditView;
	import com.iap.app.AppGlobal;
	import com.iap.app.StatusCode;
	import com.iap.common.model.Image;
	
	import spark.components.Group;
	import com.iap.common.mediator.BaseMediator;

	public class ImageEditMediator extends BaseMediator
	{
		[Inject]
		public var view:ImageEditView;
		[Inject]
		public var request:ImageRequestSignal;
		
		override public function onRegister():void
		{
			view.onCancel.add(onCancel);
			view.onSubmit.add(onSubmit);
			view.onComplete.add(onComplete);
			addContextListener(ImageEvent.RESPONSE, responseHandler);
		}
		
		private function onCancel(itemName:String):void
		{
			switch(itemName)
			{
				case ImageEditViewCode.CANCEL_NAME:
				{
					optionBack();
					break;
				}
			}
		}
		
		private function optionBack():void
		{
			setView(view.parent as Group, new ImageView(), FX_GLIDE_OUT);
		}
		
		private function onSubmit(model:Image, task:String):void
		{
			AppGlobal.startWait();
			request.dispatch(model, task);
		}

		private function onComplete(model:Image):void
		{
			AppGlobal.startWait();
			optionBack();
		}
		
		private function responseHandler(evt:ImageEvent):void
		{
			AppGlobal.endWait();
			if(evt.response.status.code == StatusCode.OK)
				optionBack();
		}
	}
}